

def drop_duplicate(ind_list):
    distinct_list = []
    for i in ind_list:
        if i not in distinct_list:
            distinct_list.append(i)
    return distinct_list


def genes_to_align(ind1, ind2):
    """
    基因二次编码
    步骤：
     1.去掉同为0部分
     2.左遍历，尾对齐
     3.右遍历，首对齐
     4.找出可合并的订单
     5.替换合并订单编号
     6.去除重复项
    :param ind1:
    :param ind2:
    :return:
    """
    # 1.去掉同为0部分
    # [1, 1, 2, 0, 0, 3, 3, 7, 9, 0, 10, 10]
    # [4, 0, 5, 5, 6, 6, 0, 0, 8, 8, 8, 0]
    i1_align, i2_align = zip(*[(i1, i2) for i1, i2 in zip(ind1, ind2) if (i1 + i2) > 0])
    i1_align = list(i1_align)
    i2_align = list(i2_align)
    print('1')
    print(i1_align)
    print(i2_align)

    # 2.左遍历，尾对齐
    # [1,1,2,2,0,3,3,7,9,9,10,10]
    # [4,4,5,5,6,6,6,0,8,8,8,8]
    i1_pre_dna = 0
    i2_pre_dna = 0
    for i in range(len(i1_align)):
        if i1_align[i] == i1_pre_dna or i2_align[i] == i2_pre_dna:
            if i1_align[i] == 0:
                i1_align[i] = i1_pre_dna
            if i2_align[i] == 0:
                i2_align[i] = i2_pre_dna
        # print("i1_align[" + str(i) + "]=" + str(i1_align[i]) + " i1_pre_dna=" + str(i1_pre_dna))
        # print("i2_align[" + str(i) + "]=" + str(i2_align[i]) + " i2_pre_dna=" + str(i2_pre_dna))
        i1_pre_dna = i1_align[i]
        i2_pre_dna = i2_align[i]
    print('2')
    print(i1_align)
    print(i2_align)

    # 3.右遍历，首对齐
    # [1, 1, 2, 2, 3, 3, 3, 7, 9, 9, 10, 10]
    # [4, 4, 5, 5, 6, 6, 6, 0, 8, 8, 8, 8]
    i1_pre_dna = 0
    i2_pre_dna = 0
    for i in range(len(i1_align)-1, -1, -1):
        if i1_align[i] == i1_pre_dna or i2_align[i] == i2_pre_dna:
            if i1_align[i] == 0:
                i1_align[i] = i1_pre_dna
            if i2_align[i] == 0:
                i2_align[i] = i2_pre_dna
        i1_pre_dna = i1_align[i]
        i2_pre_dna = i2_align[i]
    print('3')
    print(i1_align)
    print(i2_align)

    # 4.找出可合并的订单
    # [9,10]
    i1_combine_dna = []
    i1_combine_name = []
    i2_combine_dna = []
    i2_combine_name = []
    i1_set = set(i1_align)
    i2_set = set(i2_align)
    for s in i2_set:
        s_len = i2_align.count(s)
        s_start = i2_align.index(s)
        s_end = s_start + s_len
        if s_len >= 2:
            i1_combines = [i for i in i1_set if i1_align.index(i) >= s_start and i1_align.index(i) + i1_align.count(i) <= s_end]
            if len(i1_combines) > 1:
                i1_combine_dna.append(i1_combines)
                i1_combine_name.append(int('000'.join(str(i) for i in i1_combines)))

    for s in i1_set:
        s_len = i1_align.count(s)
        s_start = i1_align.index(s)
        s_end = s_start + s_len
        if s_len >= 2:
            i2_combines = [i for i in i2_set if i2_align.index(i) >= s_start and i2_align.index(i) + i2_align.count(i) <= s_end]
            if len(i2_combines) > 1:
                i2_combine_dna.append(i2_combines)
                i2_combine_name.append(int('000'.join(str(i) for i in i2_combines)))

    print('4')
    print(i1_combine_dna)
    print(i2_combine_dna)

    # 5.合并DNA
    # [1, 1, 2, 2, 3, 3, 3, 7, 900010, 900010, 900010, 900010]
    # [4, 4, 5, 5, 6, 6, 6, 0, 8, 8, 8, 8]
    for i in range(len(i1_align)):
        for j in range(len(i1_combine_dna)):
            if i1_align[i] in i1_combine_dna[j]:
                i1_align[i] = i1_combine_name[j]
    for i in range(len(i2_align)):
        for j in range(len(i2_combine_dna)):
            if i2_align[i] in i2_combine_dna[j]:
                i2_align[i] = i2_combine_name[j]
    print('5')
    print(i1_align)
    print(i2_align)

    # 6.去除重复项
    # [1, 2, 3, 7, 90010]
    # [4, 5, 6, 0, 8]
    i1_aligned = drop_duplicate(i1_align)
    i2_aligned = drop_duplicate(i2_align)
    print('6')
    return i1_aligned, i2_aligned


if __name__ == '__main__':
    # 测试基因二次编码
    ind1 = [1, 1, 1, 2, 0, 0, 3, 3, 7, 9, 0, 10, 10]
    ind2 = [4, 0, 0, 5, 5, 6, 6, 0, 0, 8, 8, 8, 0]
    i1_align, i2_align = genes_to_align(ind1, ind2)
    print(i1_align)
    print(i2_align)
